package com.zd.mole.site.mohurd.process;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.zd.mole.process.ProcessHandler;
import com.zd.mole.site.mohurd.entity.Ot_pro_shig;
import com.zd.mole.site.mohurd.repository.OtCompanyInfoRepository;
import com.zd.mole.site.mohurd.repository.OtPersonInfoRepository;
import com.zd.mole.site.mohurd.service.SysDictService;
import com.zd.mole.task.TaskStatus;
import com.zd.mole.task.entity.Task;
import com.zd.mole.task.service.TaskService;
import com.zd.mole.utils.RegexUtils;

/**
 * 项目数据/施工许可
 * @author ZhangDi
 *
 */
@Component
@Transactional
public class MohurdDataserviceQueryProjectBuildliseInfoHandler implements ProcessHandler {

	private Logger log = LoggerFactory.getLogger(getClass());
	
	@PersistenceContext 
	private EntityManager em;
	@Resource
	private SysDictService sysDictService;
	@Resource
	private OtCompanyInfoRepository otCompanyInfoRepository;
	@Resource
	private OtPersonInfoRepository otPersonInfoRepository;
	@Resource
	private TaskService taskService;
	
	@Override
	public List<Task> handler(Task task, String text) {
		Ot_pro_shig p = new Ot_pro_shig();
		String id = task.getRequestUrl().replaceFirst("/dataservice/query/project/buildliseInfo/", "");
		p.setId(id);
		p.setProjectNo(task.getCode());
		p.setShigCode(RegexUtils.find("<td data-header=\"施工许可证编号\">" + INNER_HTML + "</td>", text));
		p.setProShiCode(RegexUtils.find("<td data-header=\"省级施工许可证编号\">" + INNER_HTML + "</td>", text));
		p.setContractNo(RegexUtils.find("<td data-header=\"合同金额（万元）\">" + INNER_HTML + "</td>", text));
		//项目经理
		String projectManager = RegexUtils.find("<td data-header=\"项目经理\">\\s*<a href=\"/dataservice/query/staff/staffDetail/(\\d+)\" target=\"_blank\">" + INNER_HTML + "</a>\\s*</td>", text);
		if(!"".equals(projectManager)) {
			p.setProjectManager(projectManager);
			p.setProjectManagerName(RegexUtils.find("<td data-header=\"项目经理\">\\s*<a href=\"/dataservice/query/staff/staffDetail/\\d+\" target=\"_blank\">" + INNER_HTML + "</a>\\s*</td>", text));
			if(!otPersonInfoRepository.existsById(id)) {
				Task t = new Task();
				t.setCode(projectManager);
				t.setHostUrl(task.getHostUrl());
				t.setProcessHandlerClassName("MohurdDataserviceQueryStaffstaffDetailHandler");
				t.setRequestUrl("/dataservice/query/staff/staffDetail/" + projectManager);
				t.setStatus(TaskStatus.Ready);
				taskService.save(task);
			}
		} else {
			p.setProjectManagerName(RegexUtils.find("<td data-header=\"项目经理\">\\s*" + INNER_HTML + "\\s*</td>", text));
		}
		p.setProjectManagerCardNo(RegexUtils.find("<td data-header=\"项目经理身份证号\">" + INNER_HTML + "</td>", text));
		
		//项目总监
		String projectInspector = RegexUtils.find("<td data-header=\"项目总监\">\\s*<a href=\"/dataservice/query/staff/staffDetail/(\\d+)\" target=\"_blank\">" + INNER_HTML + "</a>\\s*</td>", text);
		if(!"".equals(projectInspector)) {
			p.setProjectInspector(projectInspector);
			p.setProjectInspectorName(RegexUtils.find("<td data-header=\"项目总监\">\\s*<a href=\"/dataservice/query/staff/staffDetail/\\d+\" target=\"_blank\">" + INNER_HTML + "</a>\\s*</td>", text));
			if(!otPersonInfoRepository.existsById(id)) {
				Task t = new Task();
				t.setCode(projectInspector);
				t.setHostUrl(task.getHostUrl());
				t.setProcessHandlerClassName("MohurdDataserviceQueryStaffstaffDetailHandler");
				t.setRequestUrl("/dataservice/query/staff/staffDetail/" + projectInspector);
				t.setStatus(TaskStatus.Ready);
				taskService.save(task);
			}
		} else {
			p.setProjectInspectorName(RegexUtils.find("<td data-header=\"项目总监\">\\s*" + INNER_HTML + "\\s*</td>", text));
		}
		p.setProjectInspectorCardNo(RegexUtils.find("<td data-header=\"项目总监身份证号\">" + INNER_HTML + "</td>", text));
		
		p.setAreaSize(RegexUtils.find("<td data-header=\"面积（平方米）\">" + INNER_HTML + "</td>", text));
		p.setCreate_date(new Date());
		String recordDate = RegexUtils.find("<td data-header=\"记录登记时间\">" + INNER_HTML + "</td>", text);
		try {
			p.setRecordDate(new SimpleDateFormat("yyyy-MM-dd").parse(recordDate));
			p.setCreate_date(p.getRecordDate());
		} catch (ParseException e) {
			log.warn("日期转换失败 {}", e.getMessage());
		}
		p.setCreate_by("1");
		p.setUpdate_date(new Date());
		p.setUpdate_by("1");
		p.setDel_flag("0");

		//勘察企业
		String surveryCompanyId = RegexUtils.find(
					"<td data-header=\"涉及单位\">勘察单位</td>\\s*<td data-header=\"企业名称\">\\s*<a href=\"/dataservice/query/comp/compDetail/(\\d+)\" target=\"_blank\">" 
					+ INNER_HTML 
					+ "\\s*</a>\\s*</td>", text);
		p.setSurveryCompanyId(surveryCompanyId);
		addCompanyTask(task, surveryCompanyId);
		if("".equals(surveryCompanyId)) {
			p.setSurveryCompanyName(RegexUtils.find(
					"<td data-header=\"涉及单位\">勘察单位</td>\\s*<td data-header=\"企业名称\">\\s*" + INNER_HTML + "\\s*</td>", text));
		}
		//设计单位
		String designCompanyId = RegexUtils.find(
				"<td data-header=\"涉及单位\">设计单位</td>\\s*<td data-header=\"企业名称\">\\s*<a href=\"/dataservice/query/comp/compDetail/(\\d+)\" target=\"_blank\">" 
						+ INNER_HTML 
						+ "\\s*</a>\\s*</td>", text);
		p.setDesignCompanyId(designCompanyId);
		addCompanyTask(task, designCompanyId);
		if("".equals(designCompanyId)) {
			p.setDesignCompanyName(RegexUtils.find(
					"<td data-header=\"涉及单位\">设计单位</td>\\s*<td data-header=\"企业名称\">\\s*"	+ INNER_HTML + "\\s*</td>", text));
		}
		//监理单位
		String supervistorCompanyId = RegexUtils.find(
				"<td data-header=\"涉及单位\">监理单位</td>\\s*<td data-header=\"企业名称\">\\s*<a href=\"/dataservice/query/comp/compDetail/(\\d+)\" target=\"_blank\">" 
						+ INNER_HTML 
						+ "\\s*</a>\\s*</td>", text);
		p.setSupervistorCompanyId(supervistorCompanyId);
		addCompanyTask(task, supervistorCompanyId);
		if("".equals(supervistorCompanyId)) {
			p.setSupervistorCompanyName(RegexUtils.find(
				"<td data-header=\"涉及单位\">监理单位</td>\\s*<td data-header=\"企业名称\">\\s*"	+ INNER_HTML + "\\s*</td>", text));
		}
		//施工单位
		String builderCompanyId = RegexUtils.find(
				"<td data-header=\"涉及单位\">施工单位</td>\\s*<td data-header=\"企业名称\">\\s*<a href=\"/dataservice/query/comp/compDetail/(\\d+)\" target=\"_blank\">" 
						+ INNER_HTML 
						+ "\\s*</a>\\s*</td>", text);
		p.setBuilderCompanyId(builderCompanyId);
		addCompanyTask(task, builderCompanyId);
		if("".equals(builderCompanyId)) {
			p.setBuilderCompanyName(RegexUtils.find(
				"<td data-header=\"涉及单位\">施工单位</td>\\s*<td data-header=\"企业名称\">\\s*" + INNER_HTML	+ "\\s*</td>", text));
		}
		em.merge(p);
		return null;
	}
	
	private void addCompanyTask(Task task, String companyId) {
		if(!"".equals(companyId)) {
			if(!otCompanyInfoRepository.existsById(companyId)) {
				Task nt = new Task();
				nt.setCode(companyId);
				nt.setHostUrl(task.getHostUrl());
				nt.setRequestUrl("/dataservice/query/comp/compDetail/" + companyId);
				nt.setProcessHandlerClassName("MohurdDataserviceQueryCompCompDetailHandler");
				nt.setStatus(TaskStatus.Ready);
				taskService.save(nt);
			}
		}
	}

}
